package com.es.hr.baoxiao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.es.common.CommonBean;
import com.es.common.CommonBiz;
import com.es.hr.baoxiaoShenpi.BaoxiaoShenpiBean;
import com.es.hr.baoxiaoShenpi.BaoxiaoShenpiBiz;
import com.es.util.mail.MailInfo;
import com.es.util.mail.SendMail;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.ActionSupport;

public class CreateBaoxiaoDoAction  extends ActionSupport
implements ServletRequestAware{
	
	HttpServletRequest request;
	
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}
	
	public String execute() throws Exception{
		String result = "";
		String message = "";
		String ctm_code = this.request.getParameter("ctm_code");
		String ctm_name = this.request.getParameter("ctm_name");
		
		String city_code = this.request.getParameter("city_code");
		String city_value = this.request.getParameter("city_value");
		
		String rtm_code = this.request.getParameter("rtm_code");
		String rtm_name = this.request.getParameter("rtm_name");
		
		String baoxiao_date = this.request.getParameter("baoxiao_date");
		
		String tongxun_fee = this.request.getParameter("tongxun_fee");
		if(tongxun_fee == null || tongxun_fee.length() == 0) tongxun_fee = "0";
		
		String za_fee = this.request.getParameter("za_fee");
		if(za_fee == null || za_fee.length() == 0) za_fee = "0";
		
		String za_beizhu = this.request.getParameter("za_beizhu");
		
		String computer_fee = this.request.getParameter("computer_fee");
		if(computer_fee == null || computer_fee.length() == 0) computer_fee = "0";
		String computer_beizhu = this.request.getParameter("computer_beizhu");
		
		String kuaidi_fee = this.request.getParameter("kuaidi_fee");
		if(kuaidi_fee == null || kuaidi_fee.length() == 0) kuaidi_fee = "0";
		String kuaidi_beizhu = this.request.getParameter("kuaidi_beizhu");
		
		String lastMouth_fee = this.request.getParameter("lastMouth_fee");
		if(lastMouth_fee == null || lastMouth_fee.length() == 0) lastMouth_fee = "0";
		String lastMouth_beizhu = this.request.getParameter("lastMouth_beizhu");
		
		String[] smallClass = (String[]) this.request.getParameterMap().get("smallClass");
		String[] kind = (String[]) this.request.getParameterMap().get("baoxiaokind");
		String[] starttime = (String[]) this.request.getParameterMap().get("starttime");
		String[] endtime = (String[]) this.request.getParameterMap().get("endtime");
		String[] fee = (String[]) this.request.getParameterMap().get("fee");
		
		
		
		ArrayList<BaoxiaoBean> baoxiaoList = new ArrayList<BaoxiaoBean>();
		BaoxiaoShenpiBean baoxiaoShenpiBean = new BaoxiaoShenpiBean();
		
		Date date = new Date();
		int year = date.getYear() + 1900;
		int mouth = date.getMonth() + 1;
		int day = date.getDate();
		
		String syear = new Integer(year).toString();
		String smouth = new Integer(mouth).toString();
		if(smouth.length() < 2) smouth = "0" + smouth;
		String sday = new Integer(day).toString();
		if(sday.length() < 2) sday = "0" + sday;
		String startTime = syear + "-" +  smouth + "-" + "00";
		String endTime = syear + "-" +  smouth + "-" + "32";
		BaoxiaoBiz baoxiaoBiz = new BaoxiaoBiz();
		boolean hasBaoxiao = baoxiaoBiz .hasBaoxiaothisMouth(ctm_code, startTime, endTime);
		if(hasBaoxiao){
			message = "每个月只能报销一次！";
			this.request.setAttribute("message", message);
			return "error";
		}
		
		//限制5号以内才能报销
		com.es.common.CommonBean riqibean = new CommonBiz().getCommonByTypeAndCode("baoxiaoproperty", "baoxiaoshenqingriqi");
		int riqi = riqibean == null?3:new Integer(riqibean.getCommonValue()).intValue();
		if(day > riqi){
			message = "每个月前"+riqi+"天才能报销";
			this.request.setAttribute("message", message);
			return "error";
		}

		float canbu_fee = 0;
		float traffic_fee = 0;
		float zhusu_fee = 0;
		if(smallClass != null){
		for(int i = 0; i < smallClass.length; ++i){
			BaoxiaoBean bean = new BaoxiaoBean();
			bean.fee_largeclass = "差旅费";
			bean.fee_smallclass = smallClass[i];
			if(bean.fee_smallclass.equals("交通") || bean.fee_smallclass.equals("餐补")){
				bean.fee_type = kind[i];
			}
			if(bean.fee_smallclass.equals("住宿")){
				bean.fee_starttime = starttime[i].trim();
				bean.fee_endtime = endtime[i].trim();
			}
			if(fee[i] == null || fee[i].length() == 0) fee[i] = "0";
			bean.fee = new Float(fee[i]).floatValue();
			baoxiaoList.add(bean);
			if( smallClass[i].equals("交通")) traffic_fee += bean.fee;
			if( smallClass[i].equals("餐补")) canbu_fee += bean.fee;
			if( smallClass[i].equals("住宿")) zhusu_fee += bean.fee;
		}
		}
		
		//常规报销
		BaoxiaoBean bean = new BaoxiaoBean();
		bean.fee_largeclass = "常规报销";
		bean.fee_smallclass = "通讯费";
		bean.fee = new Float(tongxun_fee).floatValue();
		baoxiaoList.add(bean);
		
		//杂费.杂费
		bean = new BaoxiaoBean();
		bean.fee_largeclass = "杂费";
		bean.fee_smallclass = "杂费";
		bean.fee = new Float(za_fee).floatValue();
		bean.fee_beizhu = za_beizhu;
		baoxiaoList.add(bean);
		
		bean = new BaoxiaoBean();
		bean.fee_largeclass = "杂费";
		bean.fee_smallclass = "电脑补助";
		bean.fee = new Float(computer_fee).floatValue();
		bean.fee_beizhu = computer_beizhu;
		baoxiaoList.add(bean);
		
		bean = new BaoxiaoBean();
		bean.fee_largeclass = "杂费";
		bean.fee_smallclass = "快递费";
		bean.fee = new Float(kuaidi_fee).floatValue();
		bean.fee_beizhu = kuaidi_beizhu;
		baoxiaoList.add(bean);
		
		bean = new BaoxiaoBean();
		bean.fee_largeclass = "杂费";
		bean.fee_smallclass = "补上月报销";
		bean.fee = new Float(lastMouth_fee).floatValue();
		bean.fee_beizhu = lastMouth_beizhu;
		baoxiaoList.add(bean);
		
		//附件
		String fileName = this.request.getParameter("fileName");
		int extIndex = fileName.lastIndexOf(".");
		String ext = "";
		if(extIndex == -1){
			ext = "";
		}else{
			ext = fileName.substring(extIndex);
		}
		
		if(fileName.length() > 0){
		bean = new BaoxiaoBean();
		bean.fee_largeclass = "附件";
		bean.fee_smallclass = "attach_" + ctm_code + "_" + baoxiao_date.substring(0, 7) + ext;
		baoxiaoList.add(bean);
		}
		
		BaoxiaoBiz bBiz = new BaoxiaoBiz();
		String start = baoxiao_date.substring(0, 8) + "00"; 
		String end = baoxiao_date.substring(0, 8) + "32"; 
		int index = bBiz.getBaoxiaoIndexThisMouth(ctm_code, start, end);
		for(int i = 0; i < baoxiaoList.size(); ++i){
			bean = baoxiaoList.get(i);
			bean.ctm_code = ctm_code;
			bean.ctm_name = ctm_name;
			bean.rtm_code = rtm_code;
			bean.rtm_name = rtm_name;	
			bean.city_code = city_code;
			bean.city_value = city_value;
			bean.baoxiao_index = index;
			bean.baoxiao_time = baoxiao_date;
			bean.baoxiao_lastmodify_time = baoxiao_date;
			
			bBiz.insertBaoxiao(bean);
		}
		
		//写入汇总的审批表
		baoxiaoShenpiBean.ctm_code = ctm_code;
		baoxiaoShenpiBean.ctm_name = ctm_name;	
		baoxiaoShenpiBean.rtm_code = rtm_code;
		baoxiaoShenpiBean.rtm_name = rtm_name;
		baoxiaoShenpiBean.city_code = city_code;
		baoxiaoShenpiBean.city_value = city_value;
		baoxiaoShenpiBean.baoxiao_create_index = index;
		baoxiaoShenpiBean.baoxiao_state_code = 0;
		baoxiaoShenpiBean.baoxiao_rtm_statecode = 0;
		baoxiaoShenpiBean.baoxiao_zongbu_statecode = 0;
		baoxiaoShenpiBean.baoxiao_create_time = baoxiao_date;
		baoxiaoShenpiBean.baoxiao_lastmodify_time = baoxiao_date;
		
		baoxiaoShenpiBean.canbu_fee = canbu_fee;
		baoxiaoShenpiBean.za_fee =  new Float(za_fee).floatValue();
		baoxiaoShenpiBean.computer_fee =  new Float(computer_fee).floatValue();
		baoxiaoShenpiBean.lastmouth_fee =  new Float(lastMouth_fee).floatValue();
		baoxiaoShenpiBean.traffic_fee = traffic_fee;
		baoxiaoShenpiBean.kuaidi_fee = new Float(kuaidi_fee).floatValue();
		baoxiaoShenpiBean.tongxun_fee = new Float(tongxun_fee).floatValue();
		baoxiaoShenpiBean.zhusu_fee = zhusu_fee;
		BaoxiaoUtil.setRtmMailAddress(baoxiaoShenpiBean);
		BaoxiaoUtil.setCtmMailAddress(baoxiaoShenpiBean);
		
		baoxiaoShenpiBean.chailv_fee = canbu_fee + traffic_fee + zhusu_fee;
		baoxiaoShenpiBean.total_fee = baoxiaoShenpiBean.za_fee + 
										baoxiaoShenpiBean.computer_fee + 
										baoxiaoShenpiBean.lastmouth_fee +
										baoxiaoShenpiBean.kuaidi_fee + 
										baoxiaoShenpiBean.tongxun_fee + 
										baoxiaoShenpiBean.chailv_fee;
		
		new BaoxiaoShenpiBiz().insertBaoxiaoShenpi(baoxiaoShenpiBean);
		
		//发邮件提示
		CommonBean chailv = new CommonBiz().getCommonByTypeAndCode("baoxiaoproperty", "chailvyuzhi");
		float chailv_yuzhi = chailv == null?700:new Float(chailv.getCommonValue()).floatValue();
		
		try{
		if(baoxiaoShenpiBean.chailv_fee > chailv_yuzhi){
			sendMailToRTM(baoxiaoShenpiBean);
			sendMailToZongbu(baoxiaoShenpiBean);
			this.request.setAttribute("message", "报销申请成功！\n报销申请的邮件提醒已成功发送给了事业部管理员和你的RTM！");
		}else{
			sendMailToRTM(baoxiaoShenpiBean);
			this.request.setAttribute("message", "报销申请成功！\n报销申请的邮件提醒已成功发送给了你的RTM！");
		}
		}catch(Exception e){
			this.request.setAttribute("message", "报销申请成功！\n但报销申请的邮件提醒发送失败！\n请及时联系管理员");
		}
		return "success";
	}
	
	public void sendMailToRTM(BaoxiaoShenpiBean baoxiaoShenpiBean) throws IOException{
			
			MailInfo mailinfo=new MailInfo();  
	        String [] toAddress= new String[1];
	        toAddress[0] = baoxiaoShenpiBean.rtm_mail;
	        mailinfo.setToAddress(toAddress); 
	        if(toAddress[0]==null||"".equals(toAddress[0]))
	        	return;
	        mailinfo.setSubject("报销申请"); 
	        String contentStart = "<a href='#'>";
	        String contentEnd = "</a>";
	        String content = "";
	        content += baoxiaoShenpiBean.rtm_name + " 您好，\n";
	        content += " 员工编号为 " +baoxiaoShenpiBean.ctm_code + " 姓名为 " + baoxiaoShenpiBean.ctm_name +" 的员工 申请了 " + baoxiaoShenpiBean.baoxiao_create_time.substring(0, 7) + " 的报销 \n 请您及时批复 \n";
	        
	        mailinfo.setContent(contentStart + content + contentEnd);//网页内容   
	        SendMail sm=new SendMail();   
	        sm.sendHtmlMail(mailinfo);
	}
	
	public void sendMailToZongbu(BaoxiaoShenpiBean baoxiaoShenpiBean) throws Exception{
		
		MailInfo mailinfo=new MailInfo();  
        
        ArrayList<String> mail = BaoxiaoUtil.getShiyebuMailAddress();
        String [] toAddress= new String[mail.size()];
        for(int i = 0; i < mail.size(); ++i){
        	toAddress[i] = mail.get(i);
        }
        if(toAddress.length < 1) return;
        mailinfo.setToAddress(toAddress); 
        
        mailinfo.setSubject("报销申请"); 
        String contentStart = "<a href='#'>";
        String contentEnd = "</a>";
        String content = "";
        content +=   "事业部管理员  您好，\n";
        content += " 员工编号为 " +baoxiaoShenpiBean.ctm_code + " 姓名为 " + baoxiaoShenpiBean.ctm_name +" 的员工 申请了 " + baoxiaoShenpiBean.baoxiao_create_time.substring(0, 7) + " 的报销 \n 请您及时批复 \n";
        
        mailinfo.setContent(contentStart + content + contentEnd);//网页内容   
        SendMail sm=new SendMail();   
        sm.sendHtmlMail(mailinfo);
}
	
	
}
